class CulturalHeritage::CulturalInstitutionsController < ApplicationController
  #before_filter :authenticate_user!
  # GET /cultural_heritage/cultural_institutions
  # GET /cultural_heritage/cultural_institutions.xml
  def index
    @title_view = 'Instituciones Culturales'
    @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.all
    @departments = Shared::Department.all
    @provinces = []
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cultural_heritage_cultural_institutions }
       format.json  { render :json => @cultural_heritage_cultural_institutions }
    end
  end

  # GET /cultural_heritage/cultural_institutions/1
  # GET /cultural_heritage/cultural_institutions/1.xml
  def show
    @title_view = 'Instituciones Culturales'
    @cultural_heritage_cultural_institution = CulturalHeritage::CulturalInstitution.find(params[:id])
    @json = @cultural_heritage_cultural_institution.to_gmaps4rails do |cultural_institution, marker|
      marker.infowindow  cultural_institution.define_info_window_for_marker_for_maps(root_url)
      marker.title   cultural_institution.name
      marker.sidebar "Ubicacion"
    end
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @cultural_heritage_cultural_institution }
        format.json  { render :json => @cultural_heritage_cultural_institution }
    end
  end

  # GET /cultural_heritage/cultural_institutions/new
  # GET /cultural_heritage/cultural_institutions/new.xml
  def new
    if (user_signed_in? && ([2,4].include?(current_user.role)))
      @title_view = 'Instituciones Culturales'
      @cultural_heritage_cultural_institution = CulturalHeritage::CulturalInstitution.new
      @departments = Shared::Department.all
      @provinces = []
      @districts = []
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @cultural_heritage_cultural_institution }
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # GET /cultural_heritage/cultural_institutions/1/edit
  def edit
    if (user_signed_in? && ([2,4].include?(current_user.role)))
      @title_view = 'Instituciones Culturales'
      @cultural_heritage_cultural_institution = CulturalHeritage::CulturalInstitution.find(params[:id])
      @departments = Shared::Department.all
      if ! @cultural_heritage_cultural_institution.department.blank?
        @provinces = Shared::Province.find(:all,:conditions=>["shared_provinces.deleted = ? and (shared_provinces.department = ? )",0,@cultural_heritage_cultural_institution.department])
        if ! @cultural_heritage_cultural_institution.province.blank?
          @districts = Shared::District.find(:all,:conditions=>["shared_districts.deleted = ? and (shared_districts.province = ? )",0,@cultural_heritage_cultural_institution.province])
        else
          @districts = []
        end
      else
        @provinces = []
        @districts = []
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # POST /cultural_heritage/cultural_institutions
  # POST /cultural_heritage/cultural_institutions.xml
  def create
    if (user_signed_in? && ([2,4].include?(current_user.role)))
      @title_view = 'Instituciones Culturales'
      @cultural_heritage_cultural_institution = CulturalHeritage::CulturalInstitution.new(params[:cultural_heritage_cultural_institution])
      @departments = Shared::Department.all
      if ! @cultural_heritage_cultural_institution.department.blank?
        @provinces = Shared::Province.find(:all,:conditions=>["shared_provinces.deleted = ? and (shared_provinces.department = ? )",0,@cultural_heritage_cultural_institution.department])
        if ! @cultural_heritage_cultural_institution.province.blank?
          @districts = Shared::District.find(:all,:conditions=>["shared_districts.deleted = ? and (shared_districts.province = ? )",0,@cultural_heritage_cultural_institution.province])
        else
          @districts = []
        end
      else
        @provinces = []
        @districts = []
      end
      if !(@cultural_heritage_cultural_institution.longitude.blank? or @cultural_heritage_cultural_institution.latitude.blank?)
        @cultural_heritage_cultural_institution.gmaps = true
      else
        @cultural_heritage_cultural_institution.gmaps = false
        @cultural_heritage_cultural_institution.longitude = nil
        @cultural_heritage_cultural_institution.latitude = nil
      end
      respond_to do |format|
        if @cultural_heritage_cultural_institution.save
          format.html { redirect_to(@cultural_heritage_cultural_institution, :notice => 'Se ha creado correctamente la institucion cultural.') }
          format.xml  { render :xml => @cultural_heritage_cultural_institution, :status => :created, :location => @cultural_heritage_cultural_institution }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @cultural_heritage_cultural_institution.errors, :status => :unprocessable_entity }
        end
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # PUT /cultural_heritage/cultural_institutions/1
  # PUT /cultural_heritage/cultural_institutions/1.xml
  def update
    if (user_signed_in? && ([2,4].include?(current_user.role)))
      @title_view = 'Instituciones Culturales'
      @cultural_heritage_cultural_institution = CulturalHeritage::CulturalInstitution.find(params[:id])
      @departments = Shared::Department.all
      if ! @cultural_heritage_cultural_institution.department.blank?
        @provinces = Shared::Province.find(:all,:conditions=>["shared_provinces.deleted = ? and (shared_provinces.department = ? )",0,@cultural_heritage_cultural_institution.department])
        if ! @cultural_heritage_cultural_institution.province.blank?
          @districts = Shared::District.find(:all,:conditions=>["shared_districts.deleted = ? and (shared_districts.province = ? )",0,@cultural_heritage_cultural_institution.province])
        else
          @districts = []
        end
      else
        @provinces = []
        @districts = []
      end
      if !(@cultural_heritage_cultural_institution.longitude.blank? or @cultural_heritage_cultural_institution.latitude.blank?)
        @cultural_heritage_cultural_institution.gmaps = true
      else
        @cultural_heritage_cultural_institution.gmaps = false
        @cultural_heritage_cultural_institution.longitude = nil
        @cultural_heritage_cultural_institution.latitude = nil
      end
      respond_to do |format|
        if @cultural_heritage_cultural_institution.update_attributes(params[:cultural_heritage_cultural_institution])
          format.html { redirect_to(@cultural_heritage_cultural_institution, :notice => 'Se ha actualizado correctamente la institucion cultural.') }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @cultural_heritage_cultural_institution.errors, :status => :unprocessable_entity }
        end
      end
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end

  # DELETE /cultural_heritage/cultural_institutions/1
  # DELETE /cultural_heritage/cultural_institutions/1.xml
  def destroy
    if (user_signed_in? && ([2,4].include?(current_user.role)))
      @cultural_heritage_cultural_institution = CulturalHeritage::CulturalInstitution.find(params[:id])
      mensaje = @cultural_heritage_cultural_institution.erasable
      if mensaje.blank?
        @cultural_heritage_cultural_institution.deleted = 1
        @cultural_heritage_cultural_institution.save
        respond_to do |format|
          format.html { redirect_to(cultural_heritage_cultural_institutions_url) }
          format.xml  { head :ok }
        end
      else
        respond_to do |format|
          format.html { redirect_to(cultural_heritage_cultural_institutions_url, :notice => mensaje) }
          format.xml  { head :ok }
        end
      end      
    else
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
    end
  end
  
  def change_provinces
    @cultural_heritage_cultural_institution = CulturalHeritage::CulturalInstitution.new
    @provinces = Shared::Province.find(:all,:conditions=>{:deleted=>[0],:department=>params[:id]})
    @districts = []
    render :partial=>"provinces", :layout=>false
  end

  def change_provinces_search
    @cultural_heritage_cultural_institution = CulturalHeritage::CulturalInstitution.new
    @provinces = Shared::Province.find(:all,:conditions=>{:deleted=>[0],:department=>params[:id]})
    @districts = []
    render :partial=>"provinces_search", :layout=>false
  end

  def change_districts
    @cultural_heritage_cultural_institution = CulturalHeritage::CulturalInstitution.new
    @districts = Shared::District.find(:all,:conditions=>{:deleted=>[0],:province=>params[:id]})
    render :partial=>"districts", :layout=>false
  end

  def update_table
    @search_name = params[:search_name]
    @search_field = params[:field_name]

    @search_department = params[:search_department]
    @search_province = params[:search_province]    
#    @cultural_heritage_cultural_institutions = []
    @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.all

    case @search_field
          when "Todos" then @variable = "cultural_heritage_cultural_institutions.name LIKE ? or cultural_heritage_cultural_institutions.email LIKE ? or cultural_heritage_cultural_institutions.web LIKE ? "
          when "Nombre" then @variable = "cultural_heritage_cultural_institutions.name LIKE ? "
          when "Correo"  then  @variable="cultural_heritage_cultural_institutions.email LIKE ? "
          when "Web"  then  @variable="cultural_heritage_cultural_institutions.web LIKE ?  "
    end
    if !(@search_field == "Todos")
      if ! @search_name.blank?
        if ! @search_department.blank?
          if ! @search_province.blank?
            @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>["cultural_heritage_cultural_institutions.deleted = ? and cultural_heritage_cultural_institutions.province = ? and " + @variable ,0,@search_province,'%'+@search_name+'%'])
          else
            @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>["cultural_heritage_cultural_institutions.deleted = ? and cultural_heritage_cultural_institutions.department = ? and "  + @variable ,0,@search_department,'%'+@search_name+'%'])
          end
        else
          #si no selecciono departamento no puede haber seleccionado una provincia
            @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>["cultural_heritage_cultural_institutions.deleted = ?  and " + @variable ,0,'%'+@search_name+'%'])
        end
      else
        if ! @search_department.blank?
          if ! @search_province.blank?
            @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>["cultural_heritage_cultural_institutions.deleted = ? and cultural_heritage_cultural_institutions.province = ?  ",0,@search_province])
          else
            @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>["cultural_heritage_cultural_institutions.deleted = ? and cultural_heritage_cultural_institutions.department = ?  ",0,@search_department])
          end
        else
          #si no selecciono departamento no puede haber seleccionado una provincia
        end
      end
    else
      if ! @search_name.blank?
        if ! @search_department.blank?
          if ! @search_province.blank?
            @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>["cultural_heritage_cultural_institutions.deleted = ? and cultural_heritage_cultural_institutions.province = ? and " + @variable ,0,@search_province,'%'+@search_name+'%','%'+@search_name+'%','%'+@search_name+'%'])
          else
            @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>["cultural_heritage_cultural_institutions.deleted = ? and cultural_heritage_cultural_institutions.department = ? and "  + @variable ,0,@search_department,'%'+@search_name+'%','%'+@search_name+'%','%'+@search_name+'%'])
          end
        else        
          #si no selecciono departamento no puede haber seleccionado una provincia
            @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>["cultural_heritage_cultural_institutions.deleted = ?  and " + @variable ,0,'%'+@search_name+'%','%'+@search_name+'%','%'+@search_name+'%'])
        end
      else
        if ! @search_department.blank?
          if ! @search_province.blank?
            @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>["cultural_heritage_cultural_institutions.deleted = ? and cultural_heritage_cultural_institutions.province = ?  ",0,@search_province])
          else
            @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>["cultural_heritage_cultural_institutions.deleted = ? and cultural_heritage_cultural_institutions.department = ?  ",0,@search_department])
          end
        else
          #si no selecciono departamento no puede haber seleccionado una provincia
        end
      end
    end
    render :partial => "table"
  end

  def gmaps_search
    @title_view = 'Instituciones Culturales'
    @search_name = params[:search_name]
    if ! @search_name.blank?     
      @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>["(cultural_heritage_cultural_institutions.name LIKE ? or cultural_heritage_cultural_institutions.description LIKE ? or cultural_heritage_cultural_institutions.address LIKE ? or cultural_heritage_cultural_institutions.email LIKE ? or cultural_heritage_cultural_institutions.phone LIKE ? or cultural_heritage_cultural_institutions.web LIKE ?)",'%'+@search_name+'%','%'+@search_name+'%','%'+@search_name+'%','%'+@search_name+'%','%'+@search_name+'%','%'+@search_name+'%'])
    else
      @cultural_heritage_cultural_institutions = CulturalHeritage::CulturalInstitution.find(:all,:conditions=>{:deleted=>[0]})
    end
    @json = @cultural_heritage_cultural_institutions.to_gmaps4rails do |cultural_heritage, marker|
      marker.infowindow  cultural_heritage.define_info_window_for_marker_for_maps(root_url)
      marker.title   cultural_heritage.name
      marker.sidebar cultural_heritage.name
    end
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cultural_heritage_cultural_institutions }
      format.json  { render :json => @cultural_heritage_cultural_institutions }
    end
  end
end
